package com.lisn.activityresultsapi;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import java.util.Map;

public class FirstActivity2 extends AppCompatActivity {
    private static final String TAG = FirstActivity2.class.getSimpleName();

    // 两个Activity之间交换数据
    ActivityResultLauncher activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {
            if (result.getResultCode() == RESULT_OK) {
                Intent data = result.getData();
                if (data != null) {
                    String data1 = data.getStringExtra("data");
                    Log.e(TAG, "onActivityResult: data1 = " + data1);
                }
            }
        }
    });

    // 请求运行时权限 Contract
    ActivityResultLauncher<String> requestPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), new ActivityResultCallback<Boolean>() {
        @Override
        public void onActivityResult(Boolean granted) {
            if (granted) {
                // User allow the permission.
                Log.e(TAG, "onActivityResult:  User allow the permission.");
            } else {
                // User deny the permission.
                Log.e(TAG, "onActivityResult:  User deny the permission.");
            }
        }
    });

    // 请求一组权限
    ActivityResultLauncher<String[]> requestMultiplePermissions = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
        @Override
        public void onActivityResult(Map<String, Boolean> result) {

            Log.e(TAG, "onActivityResult: " + result);
        }
    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frist2);
        findViewById(R.id.first2_button).setOnClickListener((v) -> {
            Intent intent = new Intent(this, SecondActivity2.class);
            intent.putExtra("first_send", "first send data");
            activityResultLauncher.launch(intent);
        });
        findViewById(R.id.first2_requestPermission).setOnClickListener((v) -> {
            requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE);
        });
        findViewById(R.id.first2_requestPermissionMap).setOnClickListener((v) -> {
            String[] datas = {Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.RECORD_AUDIO,
            };
            requestMultiplePermissions.launch(datas);
        });

    }
}